<template>
  <!--
    1：定义api
    2:导入api
    3:解析excel拿到数据后，调用api传入数据
    4：调用成功：
       提示成功
  -->
  <div>
    <UploadExcel :on-success="onSuccess" />
  </div>
</template>
<script>
import UploadExcel from '@/components/UploadExcel'
import { sysUserBatch } from '@/api/employees'
export default {
  components: {
    UploadExcel
  },
  methods: {
    changeTime(num) {
      const t = new Date((num - 1) * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000)
      t.setYear(t.getFullYear() - 70)
      return t
    },
    async onSuccess(res) {
      // timeOfEntry	string	非必须		入职时间
      // username	string	非必须		姓名
      // workNumber	string	非必须		工号
      // mobile	string	非必须		手机号
      // correctionTime	string	非必须		转正时间
      const result = res.results.map((item) => {
        return {
          timeOfEntry: this.changeTime(item['入职日期']),
          username: item['姓名'],
          workNumber: item['工号'],
          mobile: item['手机号'],
          correctionTime: this.changeTime(item['转正日期'])
        }
      })
      await sysUserBatch(result)
      this.$message.success('导入成功')
      this.$router.push('/employees')
      //  this.$router.back(0/-1/1)
      // this.$router.go(0/-1/1)

      console.log(result)
    }
  }
}
</script>
<style></style>
